﻿using System;
using System.Collections.Generic;
using System.Linq;
using FacialClub.Helpers;

namespace FacialClub.Helpers
{
    /// <summary>
    /// Paged List Interface
    /// </summary>
    public interface IPagedList
    {
        /// <summary>
        /// Total Pages
        /// </summary>
        int TotalPages
        {
            get;
            set;
        }

        /// <summary>
        /// Total Count
        /// </summary>
        int TotalCount
        {
            get;
            set;
        }

        /// <summary>
        /// Page Index
        /// </summary>
        int PageIndex
        {
            get;
            set;
        }

        /// <summary>
        /// Page Size
        /// </summary>
        int PageSize
        {
            get;
            set;
        }

        /// <summary>
        /// Is Previous Page
        /// </summary>
        bool HasPreviousPage
        {
            get;
        }

        /// <summary>
        /// Is Next Page
        /// </summary>
        bool HasNextPage
        {
            get;
        }
    }

    /// <summary>
    /// PagedListCollection
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class PaginatedListCollection<T> : List<T>, IPagedList
    {
        /// <summary>
        /// Paged List
        /// </summary>
        /// <param name="source"></param>
        /// <param name="index"></param>
        /// <param name="pageSize"></param>
        public PaginatedListCollection(IQueryable<T> source, int index, int pageSize)
        {
            this.TotalCount = source.Count();
            this.PageSize = pageSize;
            this.PageIndex = index;
            this.AddRange(source.Skip(index * pageSize).Take(pageSize).ToList());
        }

        /// <summary>
        /// PagedListCollection
        /// </summary>
        /// <param name="source"></param>
        /// <param name="index"></param>
        /// <param name="pageSize"></param>
        public PaginatedListCollection(IEnumerable<T> source, int index, int pageSize)
        {
            this.TotalCount = source.Count();
            this.PageSize = pageSize;
            this.PageIndex = index;
            this.AddRange(source.Skip(index * pageSize).Take(pageSize).ToList());
        }

        ///<summary>
        /// Total Pages
        /// </summary>
        public int TotalPages
        {
            get;
            set;
        }

        /// <summary>
        /// Total Count
        /// </summary>
        public int TotalCount
        {
            get;
            set;
        }

        /// <summary>
        /// Page Index
        /// </summary>
        public int PageIndex
        {
            get;
            set;
        }

        /// <summary>
        /// Page Size
        /// </summary>
        public int PageSize
        {
            get;
            set;
        }

        /// <summary>
        /// Has Previous Page
        /// </summary>
        public bool HasPreviousPage
        {
            get
            {
                return (PageIndex > 1);
            }
        }

        /// <summary>
        /// Has Next Page
        /// </summary>
        public bool HasNextPage
        {
            get
            {
                return (PageIndex * PageSize) <= TotalCount;
            }
        }
    }

    /// <summary>
    /// Pagination
    /// </summary>
    public static class Pagination
    {
        /// <summary>
        /// ToPagedList
        /// </summary>
        /// <param name="source"></param>
        /// <param name="index"></param>
        /// <param name="pageSize"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static PaginatedListCollection<T> ToPagedList<T>(this IQueryable<T> source, int index, int pageSize)
        {
            return new PaginatedListCollection<T>(source, index, pageSize);
        }

        /// <summary>
        /// ToPagedList
        /// </summary>
        /// <param name="source"></param>
        /// <param name="index"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static PaginatedListCollection<T> ToPagedList<T>(this IQueryable<T> source, int index)
        {
            return new PaginatedListCollection<T>(source, index, 10);
        }
    }
}

